package com.mdd.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.product.entity.LeasingSchemeDetails;
import com.mdd.product.mapper.LeasingSchemeDetailsMapper;
import com.mdd.product.service.ILeasingSchemeDetailsService;
import com.mdd.product.validate.LeasingSchemeDetailsCreateValidate;
import com.mdd.product.validate.LeasingSchemeDetailsUpdateValidate;
import com.mdd.product.vo.LeasingSchemeDetailsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * 租赁方案详细实现类
 * @author LikeAdmin
 */
@Service
public class LeasingSchemeDetailsServiceImpl implements ILeasingSchemeDetailsService {

    @Resource
    LeasingSchemeDetailsMapper leasingSchemeDetailsMapper;

    /**
     * 租赁方案详细列表
     *
     * @author LikeAdmin
     * @param spuId 商品id
     * @return PageResult<OmsLeasingSchemeDetilsListedVo>
     */
    @Override
    public List<LeasingSchemeDetailsVo> list(Long spuId) {
        LambdaQueryWrapper<LeasingSchemeDetails> leasingSchemeDetailsQueryWrapper=new LambdaQueryWrapper<>();
        leasingSchemeDetailsQueryWrapper.eq(LeasingSchemeDetails::getSpuId,spuId);
        List<LeasingSchemeDetails> leasingSchemeDetails = leasingSchemeDetailsMapper.selectList(leasingSchemeDetailsQueryWrapper);

        List<LeasingSchemeDetailsVo> list = new LinkedList<>();
        for(LeasingSchemeDetails item : leasingSchemeDetails) {
            LeasingSchemeDetailsVo vo = new LeasingSchemeDetailsVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return list;
    }

    /**
     * 租赁方案详细详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OmsLeasingSchemeDetils
     */
    @Override
    public LeasingSchemeDetailsVo detail(Long id) {
        LeasingSchemeDetails model = leasingSchemeDetailsMapper.selectOne(
                new QueryWrapper<LeasingSchemeDetails>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "租赁方案详细详情数据不存在");

        LeasingSchemeDetailsVo vo = new LeasingSchemeDetailsVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 租赁方案详细新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(LeasingSchemeDetailsCreateValidate createValidate) {
        LeasingSchemeDetails model = new LeasingSchemeDetails();
        model.setSpuId(createValidate.getSpuId());
        model.setSchemeId(createValidate.getSchemeId());
        model.setLeaseType(createValidate.getLeaseType());
        model.setName(createValidate.getName());
        model.setPeriodicity(createValidate.getPeriodicity());
        model.setAmountPeriod(createValidate.getAmountPeriod());
        model.setDeposit(createValidate.getDeposit());
        model.setDownPayments(createValidate.getDownPayments());
        model.setOtherExpenses(createValidate.getOtherExpenses());
        model.setNeedDamages(createValidate.getNeedDamages());
        model.setGraceDays(createValidate.getGraceDays());
        model.setPenaltyRatio(createValidate.getPenaltyRatio());
        leasingSchemeDetailsMapper.insert(model);
    }

    /**
     * 租赁方案详细编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(LeasingSchemeDetailsUpdateValidate updateValidate) {
        LeasingSchemeDetails model = leasingSchemeDetailsMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "租赁方案详细数据不存在!");
        model.setSchemeId(updateValidate.getSchemeId());
        model.setSpuId(updateValidate.getSpuId());
        model.setLeaseType(updateValidate.getLeaseType());
        model.setName(updateValidate.getName());
        model.setPeriodicity(updateValidate.getPeriodicity());
        model.setAmountPeriod(updateValidate.getAmountPeriod());
        model.setDeposit(updateValidate.getDeposit());
        model.setDownPayments(updateValidate.getDownPayments());
        model.setOtherExpenses(updateValidate.getOtherExpenses());
        model.setNeedDamages(updateValidate.getNeedDamages());
        model.setGraceDays(updateValidate.getGraceDays());
        model.setPenaltyRatio(updateValidate.getPenaltyRatio());
        leasingSchemeDetailsMapper.updateById(model);
    }

    /**
     * 租赁方案详细删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        LeasingSchemeDetails model = leasingSchemeDetailsMapper.selectById(id);

        Assert.notNull(model, "租赁方案详细数据不存在!");

        leasingSchemeDetailsMapper.deleteById(id);
    }

    @Override
    public void delBySchemeIds(List schemeId) {
        LambdaQueryWrapper<LeasingSchemeDetails> propertyValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
        propertyValueLambdaQueryWrapper.in(LeasingSchemeDetails::getSchemeId,schemeId);
        leasingSchemeDetailsMapper.delete(propertyValueLambdaQueryWrapper);
    }

    @Override
    public void delBySchemeIdAndNotInValues(Long schemeId, Set<Long> values) {
        QueryWrapper<LeasingSchemeDetails> queryWrapper = new QueryWrapper();
        queryWrapper.eq("scheme_id",schemeId);
        if(!values.isEmpty()){
            queryWrapper.notIn("id",values);
        }
        leasingSchemeDetailsMapper.delete(queryWrapper);
    }

}
